package vn.gameloft.egg.model;

import java.util.ArrayList;
import java.util.List;

import vn.gameloft.egg.MainScreen;

import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class Chick extends GameObject {

    // create egg time.
    public static final int CREATE_EGG_TIME = 5;

    private List<Egg> eggs = new ArrayList<Egg>();
    private List<Egg> tmpEggList = new ArrayList<Egg>();

    private float eggTime = 0;

    private float moveSpeed;

    public Basket ro = null;

    public Chick(FileHandle img) {
        super(img);
        moveSpeed = (float) (Math.random() * 100 + 50);
    }

    public void redraw(SpriteBatch batch, float delta) {
        if (this.x <= 10 && this.moveSpeed < 0
                || this.x + this.getWidth() + 10 >= MainScreen.SCREEN_WIDTH
                && this.moveSpeed > 0) {
            moveSpeed = -moveSpeed;
        }

        this.x += moveSpeed * delta;
        this.draw(batch);
        eggTime += delta;
        if (eggTime > 5) {
            boolean isChatThai = Math.random() > 0.8;
            Egg egg = null;
            if (tmpEggList.size() == 0) {

                egg = new Egg(isChatThai);

            } else {
                egg = tmpEggList.remove(0);
                egg.setChatThai(isChatThai);
            }
            egg.x = this.x + this.getWidth() / 2 - egg.getWidth() / 2;
            egg.y = this.y - egg.getHeight();
            this.eggs.add(egg);
            eggTime = 0;
        }

        for (int i = eggs.size() - 1; i >= 0; i--) {
            Egg egg = eggs.get(i);
            egg.y -= ro.eggSpeed * delta;
            if (ro.isOverlap(egg)) {
                ro.setPoint(egg.isChatThai);
                this.tmpEggList.add(egg);
                eggs.remove(i);
            } else if (egg.y <= 0) {
                this.eggs.remove(i);
                this.tmpEggList.add(egg);
            } else {
                egg.draw(batch);
            }
        }
    }
}
